/**
 * basic configs
 * common utils
 */

function urlArgs(uargs, url) {
	var p='', s='';
	if (url) {
		var uparts = url.split('?');
		p = uparts[0] ? uparts[0] : '';
		s = uparts[1] ? uparts[1] : '';
	} else {
		p = window.location.pathname;
		s = window.location.search.substring(1);
	}
	p += '?';
	var pairs = s.split('&');
	var args = {};
	var i, k;
	for (i in pairs) {
		var arr = pairs[i].split('=');
		if (arr[0]) args[arr[0]] = arr[1] ? arr[1] : '';
	}
	for (k in uargs) {
		args[k] = uargs[k];
	}
	for (k in args) {
		p += k+'='+args[k]+'&';
	}
	return p.slice(0, -1);
}

function iresizeinit() {
	$('.j-resize').each(function () {
		var _self = $(this);
		var whd = _self.attr('data-cfg');
		if (typeof(whd) != 'string') return true;
		whd = whd.split('x');
		var width = whd[0];
		var height = whd[1];
		if (!(width>0 && height>0)) return true;
		var mode = whd[2];
		var i = width / height;
		//_self.wrap('<span></span>');
		_self.parent().css('display', 'block').css('width', width+'px').css('height', height+'px').css('position', 'relative').css('overflow', 'hidden');
		var imgloader = new Image();
		$(imgloader).load(function () {
			var tw = imgloader.width;
			var th = imgloader.height;
			var ti = tw / th;
			var rw = 0;
			var rh = 0;
			if (mode == 'lzo') {
				if (ti > i) {
					rw = width;
					rh = Math.round(width / ti);
				} else {
					rw = Math.round(height * ti);
					rh = height;
				}
			} else if (mode == 'szo') {
				if (ti > i) {
					rw = Math.round(height * ti);
					rh = height;
				} else {
					rw = width;
					rh = Math.round(width / ti);
				}
			} else if (mode == 'lzi') {
				if (ti > i) {
					if (tw > width) {
						rw = width;
						rh = Math.round(width / ti);
					} else {
						rw = tw;
						rh = th;
					}
				} else {
					if (th > height) {
						rw = Math.round(height * ti);
						rh = height;
					} else {
						rw = tw;
						rh = th;
					}
				}
			} else if (mode == 'szi') {
				if (ti < i) {
					if (tw > width) {
						rw = width;
						rh = Math.round(width / ti);
					} else {
						rw = tw;
						rh = th;
					}
				} else {
					if (th > height) {
						rw = Math.round(height * ti);
						rh = height;
					} else {
						rw = tw;
						rh = th;
					}
				}
			} else {
				return true;
			}
			_self.width(rw);
			_self.height(rh);
			_self.css('position', 'absolute');
			_self.css('left', Math.round((width-rw)/2)+'px');
			_self.css('top', Math.round((height-rh)/2)+'px');
		}).attr('src', _self.attr('src'));
	});
}

$(function(){
	// 图片大小重置
	iresizeinit();
	// 下拉菜单
	var j_menu_t;
	$('.j-menu').mouseover(function(){
		j_menu_t = clearInterval(j_menu_t);
		$(this).find('.j-menuul').show();
	}).mouseout(function(){
		var _self = this;
		j_menu_t = setTimeout(function(){
			$(_self).find('.j-menuul').hide();
		}, 500);
	});
});